Skip to content

fix(feature_flags): handle expected falsy values in conditions #2052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 27, 2023

Conversation

ran-isenberg
Copy link
Contributor

@ran-isenberg ran-isenberg commented Mar 26, 2023

Issue number: #2051

Summary

Rule Conditions allow users to evaluate context attributes against values and actions defined in Rule Conditions.
The condition can easily be pivoted on a certain value in the context evaluating to falsy values
The values attribute should be able to accept any value except null
The condition should also honor any value for context key variable.

Changes

  • Added tests
  • changed the validation from if true to if none
  • changed error message to none instead of empty

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@ran-isenberg ran-isenberg requested a review from a team as a code owner March 26, 2023 14:08
@ran-isenberg ran-isenberg requested review from heitorlessa and removed request for a team March 26, 2023 14:08
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 26, 2023
@boring-cyborg boring-cyborg bot added the tests label Mar 26, 2023
@github-actions github-actions bot added the bug Something isn't working label Mar 27, 2023
@heitorlessa heitorlessa changed the title fix: Feature Flag Rule Conditions Values and Context Key Variables ca… fix(feature_flags): handle falsy values when they're meant to be falsy Mar 27, 2023
@heitorlessa heitorlessa changed the title fix(feature_flags): handle falsy values when they're meant to be falsy fix(feature_flags): handle expected falsy values in conditions Mar 27, 2023
Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great, thanks for the additional tests!

Only a minor semantic correction, pushing from GitHub UI so we can make a patch release.

@heitorlessa
Copy link
Contributor

hmmm, GitHub review is messing up indentation and isn't signing off commit to push the change. Can't push to your fork either - here's the patch with the changes whenever you can.

From bd8a6215e4815cc729d0c695d42a14144bc56ee7 Mon Sep 17 00:00:00 2001
From: heitorlessa <[email protected]>
Date: Mon, 27 Mar 2023 14:26:08 +0200
Subject: [PATCH] chore: use null instead of none in err msg

---
 aws_lambda_powertools/utilities/feature_flags/schema.py  | 2 +-
 tests/functional/feature_flags/test_schema_validation.py | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/aws_lambda_powertools/utilities/feature_flags/schema.py b/aws_lambda_powertools/utilities/feature_flags/schema.py
index df999a51..08f2ee13 100644
--- a/aws_lambda_powertools/utilities/feature_flags/schema.py
+++ b/aws_lambda_powertools/utilities/feature_flags/schema.py
@@ -327,7 +327,7 @@ class ConditionsValidator(BaseValidator):
     def validate_condition_value(condition: Dict[str, Any], rule_name: str):
         value = condition.get(CONDITION_VALUE)
         if value is None:
-            raise SchemaValidationError(f"'value' key must not be none, rule={rule_name}")
+            raise SchemaValidationError(f"'value' key must not be null, rule={rule_name}")
         action = condition.get(CONDITION_ACTION, "")
 
         # time actions need to be parsed to make sure date and time format is valid and timezone is recognized
diff --git a/tests/functional/feature_flags/test_schema_validation.py b/tests/functional/feature_flags/test_schema_validation.py
index 8441b944..8d3b97ad 100644
--- a/tests/functional/feature_flags/test_schema_validation.py
+++ b/tests/functional/feature_flags/test_schema_validation.py
@@ -301,7 +301,7 @@ def test_validate_condition_missing_condition_value():
     }
 
     # WHEN calling validate_condition
-    with pytest.raises(SchemaValidationError, match="'value' key must not be none"):
+    with pytest.raises(SchemaValidationError, match="'value' key must not be null"):
         ConditionsValidator.validate_condition_value(condition=condition, rule_name="dummy")
 
 
@@ -314,7 +314,7 @@ def test_validate_condition_none_condition_value():
     }
 
     # WHEN calling validate_condition
-    with pytest.raises(SchemaValidationError, match="'value' key must not be none"):
+    with pytest.raises(SchemaValidationError, match="'value' key must not be null"):
         ConditionsValidator.validate_condition_value(condition=condition, rule_name="dummy")
 
 
-- 
2.37.1 (Apple Git-137.1)

@heitorlessa
Copy link
Contributor

hmmm, GitHub review is messing up indentation and isn't signing off commit to push the change. Can't push to your fork either - here's the git patch with the changes whenever you can.

From bd8a6215e4815cc729d0c695d42a14144bc56ee7 Mon Sep 17 00:00:00 2001
From: heitorlessa <[email protected]>
Date: Mon, 27 Mar 2023 14:26:08 +0200
Subject: [PATCH] chore: use null instead of none in err msg

---
 aws_lambda_powertools/utilities/feature_flags/schema.py  | 2 +-
 tests/functional/feature_flags/test_schema_validation.py | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/aws_lambda_powertools/utilities/feature_flags/schema.py b/aws_lambda_powertools/utilities/feature_flags/schema.py
index df999a51..08f2ee13 100644
--- a/aws_lambda_powertools/utilities/feature_flags/schema.py
+++ b/aws_lambda_powertools/utilities/feature_flags/schema.py
@@ -327,7 +327,7 @@ class ConditionsValidator(BaseValidator):
     def validate_condition_value(condition: Dict[str, Any], rule_name: str):
         value = condition.get(CONDITION_VALUE)
         if value is None:
-            raise SchemaValidationError(f"'value' key must not be none, rule={rule_name}")
+            raise SchemaValidationError(f"'value' key must not be null, rule={rule_name}")
         action = condition.get(CONDITION_ACTION, "")
 
         # time actions need to be parsed to make sure date and time format is valid and timezone is recognized
diff --git a/tests/functional/feature_flags/test_schema_validation.py b/tests/functional/feature_flags/test_schema_validation.py
index 8441b944..8d3b97ad 100644
--- a/tests/functional/feature_flags/test_schema_validation.py
+++ b/tests/functional/feature_flags/test_schema_validation.py
@@ -301,7 +301,7 @@ def test_validate_condition_missing_condition_value():
     }
 
     # WHEN calling validate_condition
-    with pytest.raises(SchemaValidationError, match="'value' key must not be none"):
+    with pytest.raises(SchemaValidationError, match="'value' key must not be null"):
         ConditionsValidator.validate_condition_value(condition=condition, rule_name="dummy")
 
 
@@ -314,7 +314,7 @@ def test_validate_condition_none_condition_value():
     }
 
     # WHEN calling validate_condition
-    with pytest.raises(SchemaValidationError, match="'value' key must not be none"):
+    with pytest.raises(SchemaValidationError, match="'value' key must not be null"):
         ConditionsValidator.validate_condition_value(condition=condition, rule_name="dummy")
 
 
-- 
2.37.1 (Apple Git-137.1)

Copy link
Contributor

@ajwad-shaikh ajwad-shaikh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@heitorlessa @ran-isenberg

There are a few tests that need fix - the expected value is the same as non-expected value which means that these tests will never fail

  1. test_flags_not_equal_match
  2. test_flags_less_than_match
  3. test_flags_less_than_or_equal_match_1
  4. test_flags_less_than_or_equal_match_2
  5. test_flags_greater_than_match
  6. test_flags_greater_than_or_equal_match_1
  7. test_flags_greater_than_or_equal_match_2

Apart from these the sample test case in the documentation also has the same flaw.

I'll be happy to raise a separate PR for this but since fixing one of these tests would have failed if did not have the flaw, I thought we can ship these together under common context. Let me know your thoughts

@heitorlessa
Copy link
Contributor

@ajwad-shaikh had an idea now that GitHub seems to be back -- create a new PR (I'm merging this shortly), so we can make sure you're credited properly in your first contribution <3

Signed-off-by: Heitor Lessa <[email protected]>
Signed-off-by: Heitor Lessa <[email protected]>
Signed-off-by: Heitor Lessa <[email protected]>
@heitorlessa
Copy link
Contributor

For future reference until we manage to create issues for these, two things I'd love to do/get help in Feature Flags:

  • Move most of the functional tests as unit tests (e.g., these Schema Validation are largely unit tests)
  • Investigate slowness in testing Feature Flags and refactor accordingly (e.g., we're always creating mapping_by_action lambda functions instead of making them constant) -- Feature Flags IIRC has the slowest test execution, py-spy should help produce a flame graph when running the test suite here

@heitorlessa
Copy link
Contributor

heitorlessa commented Mar 27, 2023

Pinged you on Discord, I'll wait for the separate PR before we make a release. If you get busy, we'll create one by EOW and credit accordingly either way... but I'd really love to make sure you get your experience in making your first contribution ;)

Merging - thanks again @ran-isenberg !

@heitorlessa heitorlessa merged commit ab9078c into aws-powertools:develop Mar 27, 2023
@ran-isenberg ran-isenberg deleted the fix-2051 branch March 27, 2023 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants